/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Control.Control;
import Interfaces.IDAOs;
import com.google.gson.Gson;

import displays.Accion;
import exceptions.PersistenciaException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author QUINTERO
 */
@WebServlet(name = "ConsultaCartelera", urlPatterns = {"/ConsultaCartelera"})
public class ConsultaCartelera extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        StringBuilder sb = new StringBuilder();
        String s;
        try {
            
        
//            System.out.println(request.getReader().readLine());
        while ((s = request.getReader().readLine()) != null) {
            sb.append(s);
            System.out.println("entro: " + sb.toString());
        }
        Gson gson = new Gson();
        Accion accion = (Accion) gson.fromJson(sb.toString(), Accion.class); //Este recive el tipo de accion que se hara
        switch (accion.getAccion()) {
            case 1:
                consultaCiudades(request, response, gson);
                break;
            case 2:
                consultaEmpresasPorCiudad(request, response, gson, accion.getCiudad());
                break;
            case 3:
                consultaSucursales(request, response, gson, accion.getCiudad(), accion.getEmpresa());
                break;
            case 4:
                consultaCartelera(request, response, gson, accion.getCine());
                break;
        }
        } catch (Exception e) {
            Logger.getLogger(ConsultaCartelera.class.getName()).log(Level.SEVERE, null, e);
            out.print(e.getMessage());
            response.setStatus(500);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void consultaCiudades(HttpServletRequest request, HttpServletResponse response, Gson gson) throws IOException, PersistenciaException {
        PrintWriter out = response.getWriter();
        IDAOs control = new Control();
        Object resultado[] = control.searchCitys().toArray();
        response.setContentType("application/json");
        out.print(gson.toJson(resultado));
        out.flush();
    }

    private void consultaEmpresasPorCiudad(HttpServletRequest request, HttpServletResponse response, Gson gson, int city) throws IOException, PersistenciaException {
        PrintWriter out = response.getWriter();
        IDAOs control = new Control();
        Object resultado[] = control.searchSubscribersByCity(city).toArray();
        response.setContentType("application/json");
        out.print(gson.toJson(resultado));
        out.flush();
    }
    
    private void consultaSucursales(HttpServletRequest request, HttpServletResponse response, Gson gson, int city, int subscriber) throws IOException, PersistenciaException {
        PrintWriter out = response.getWriter();
        IDAOs control = new Control();
        Object resultado[] = control.searchLocationsBySub(city, subscriber).toArray();
        response.setContentType("application/json");
        out.print(gson.toJson(resultado));
        out.flush();
    }
    
    private void consultaCartelera(HttpServletRequest request, HttpServletResponse response, Gson gson, int location) throws IOException, PersistenciaException {
        PrintWriter out = response.getWriter();
        IDAOs control = new Control();
        Object resultado = control.searchBillBoardByLocation(location);
        response.setContentType("application/json");
        out.print(gson.toJson(resultado));
        out.flush();
    }
}
